import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import LoginView from '@/views/LoginView.vue'
import RegisterView from '@/views/RegisterView.vue'
import { userRoutes } from '@/router/user'
// 移除鉴权所需的 store 引用，仅保留标题设置

// 基础路由配置
const baseRoutes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/HomeView.vue'),
    meta: { title: '首页' }
  },

  {
    path: '/login',
    name: 'login',
    component: LoginView,
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView,
    meta: { title: '注册' }
  }
]

// 合并所有路由配置
const routes: RouteRecordRaw[] = [
  ...baseRoutes,
  ...userRoutes,

]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 路由守卫示例（可选）
router.beforeEach((to, from, next) => {
  console.debug('[router] navigating from', from.fullPath, 'to', to.fullPath)
  if (to.meta?.title) {
    document.title = to.meta.title as string
  }
  next()
})

router.afterEach((to, from) => {
  console.debug('[router] afterEach, now at', to.fullPath, 'from', from.fullPath)
})

export default router